package appController

import (
	"CMETO/models"
	"net/http"

	"github.com/gin-gonic/gin"
)

// app部分的接口
type GoodsController struct{}

// @Summary 商品查询
// @Description 根据id查商品
// @Tags APP管理
// @Accept json
// @Produce json
// @param id query string false "商品id"
// @Success 200 {object} models.ResultCommon[[]models.Goods] "查询成功"
// @Success 401 {object} models.ResultCommon[string] "查询失败"
// @Router /app/goods [get]
// @Security JWT
func (r GoodsController) QueryGoods(ctx *gin.Context) {
	nameStr := ctx.DefaultQuery("id", "0")
	var goods models.Goods
	err := models.DB.Where("id = ?", nameStr).First(&goods).Error
	if err != nil {
		ctx.JSON(http.StatusOK, models.ResultCommon[string]{
			Msg:    "查询失败",
			Code:   "400",
			Result: "",
		})
	} else {
		ctx.JSON(http.StatusOK, models.ResultCommon[models.Goods]{
			Msg:    "查询成功",
			Code:   "200",
			Result: goods,
		})
	}
}

// @Summary 商品查询
// @Description 根据分类查商品
// @Tags APP管理
// @Accept json
// @Produce json
// @param id query string false "分类id"
// @Success 200 {object} models.ResultCommon[[]models.Goods] "查询成功"
// @Success 401 {object} models.ResultCommon[string] "查询失败"
// @Router /app/goods/bysort [get]
// @Security JWT
func (r GoodsController) QueryGoodsBySort(ctx *gin.Context) {
	nameStr := ctx.DefaultQuery("id", "0")
	var goodsList []models.Goods
	err := models.DB.Where("sid = ?", nameStr).Find(&goodsList).Error
	if err != nil {
		ctx.JSON(http.StatusOK, models.ResultCommon[string]{
			Msg:    "查询失败",
			Code:   "400",
			Result: "",
		})
	} else {
		ctx.JSON(http.StatusOK, models.ResultCommon[[]models.Goods]{
			Msg:    "查询成功",
			Code:   "200",
			Result: goodsList,
		})
	}
}
